package dao;

import bean.Reader;
import db.MyHibernateSessionFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import java.util.List;

/**
 * Created by WeaponLin on 2015/11/19.
 */
public class ReaderDAO {

    public List<Reader> queryReaderBySql(String sql) {
        Transaction transaction = null;
        List<Reader> list = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            transaction = session.beginTransaction();// 事务开启
            String sqlStr = "from Reader where " + sql;
            Query query = session.createQuery(sqlStr);
            list = query.list();
            transaction.commit();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            transaction.commit();
            return list;
        }
    }

    public List<Reader> queryReaderBySQL(String sql) {
        Transaction transaction = null;
        List<Reader> list = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            transaction = session.beginTransaction();// 事务开启
            Query query = session.createSQLQuery(sql).addEntity(Reader.class);
            list = query.list();
            transaction.commit();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            transaction.commit();
            return list;
        }
    }

    public boolean addReader(Reader reader) {
        Transaction transaction = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            transaction = session.beginTransaction();// 事务开启
            session.save(reader);
            transaction.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            transaction.commit();
            return false;
        }
    }

    public boolean updateReader(Reader reader) {
        Transaction transaction = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            transaction = session.beginTransaction();// 事务开启
            session.update(reader);
            transaction.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            transaction.commit();
            return false;
        }
    }

    public boolean deleteReader(String readerid) {
        Transaction transaction = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            transaction = session.beginTransaction();
            Query query = session.createQuery("from Reader where readerid='"
                    + readerid + "'");
            List<Reader> list = query.list();
            if(list.size() > 0){
                session.delete(list.get(0));
            }
            transaction.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            transaction.commit();
            return false;
        }
    }
}
